/**
 * 逻辑、功能相关描述: <br>
 *
 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>  <br>
 * 编写日期：2008-10-22  <br>
 */
package com.ce.pms.common.dao.impl;

import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.common.dao.ISecUserDao;
import com.ce.pms.common.model.SecUser;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 名称: com.ce.pms.common.dao.impl.SecUserDaoImpl <br>
 *
 *
 * @author <a href="mailto:i0o@live.cn">黄腾飞</a> <br>
 *         Created on： 2008-10-22 <br>
 * @author 付湘辉 <br>
 *         update on： 2008-10-30 <br>
 *         why: 将类名从SecUserDao改为SecUserDaoImpl，增加方法实现queryUserNameById<br>
 */
public class SecUserDaoImpl extends PmsBaseDaoImpl<SecUser> implements
		ISecUserDao {

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ce.pms.common.dao.ISecUserDao#getEmployeeList()
	 */
	public Page getEmployeeList(String userCode, String employeeName,
			Integer pageNO, Integer pageSize) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("SELECT ");
		buffer.append("HME.EMPLOYEE_ID, ");
		buffer.append("HME.DEPT_ID, ");
		buffer.append("HME.EMPLOYEE_NAME, ");
		buffer.append("HME.SEX, ");
		buffer.append("HME.ID_TYPE_ID, ");
		buffer.append("HME.ID_NO, ");
		buffer.append("SU.USER_ID, ");
		buffer.append("SU.USERCODE, ");
		buffer.append("HMD.DEPT_NAME, ");
		buffer.append("CIT.ID_TYPE_NAME ");
		buffer.append("FROM HM_EMPLOYEE AS HME ");
		buffer
				.append("INNER JOIN SEC_USER AS SU ON SU.EMPLOYEE_ID = HME.EMPLOYEE_ID ");
		buffer.append("INNER JOIN HM_DEPT AS HMD ON HME.DEPT_ID=HMD.DEPT_ID ");
		buffer
				.append("LEFT JOIN CS_ID_TYPE AS CIT ON HME.ID_TYPE_ID = CIT.ID_TYPE_ID ");
		buffer.append("WHERE 1=1 AND SU.USERCODE LIKE  ? ");
		buffer.append(" AND HME.EMPLOYEE_NAME LIKE  ? ");

		// Query query = super.createSQLQuery(sql, null);

		return super
				.pagedSqlQueryByPageNo(buffer.toString(), pageNO, pageSize,
						new Object[] { "%" + userCode + "%",
								"%" + employeeName + "%" });
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ce.pms.common.dao.ISecUserDao#deleteDataRightByUserId(java.lang.Long)
	 */
	public boolean deleteDataRightByUserId(Long userId) {
		String sqlStr = "SELECT SUR.RIGHT_ID FROM SEC_USER_R_RIGTHS AS SUR WHERE SUR.USER_ID = "
				+ userId;
		Query query = super.createSQLQuery(sqlStr, null);

		List surList = query.list();
		for (int i = 0; i < surList.size(); i++) {
			BigInteger rightId = (BigInteger) surList.get(i);
			super.createSQLQuery(
					"DELETE FROM SEC_USER_R_RIGTHS  WHERE USER_ID = ?",
					new Long[] { userId }).executeUpdate();
			super
					.createSQLQuery(
							"DELETE FROM SEC_RIGHT_BUILDING WHERE BUILDING_RIGHT_ID = ?",
							new BigInteger[] { rightId }).executeUpdate();
			super
					.createSQLQuery(
							"DELETE FROM SEC_RIGHT_PRECINCT WHERE RIGHT_PRECINCT_ID = ?",
							new BigInteger[] { rightId }).executeUpdate();
			super.createSQLQuery("DELETE FROM SEC_RIGHT WHERE RIGHT_ID = ?",
					new BigInteger[] { rightId }).executeUpdate();
		}
		return true;
	}

	/**
	 * 功能描述: 根据用户id查询用户登陆名(SQL查询) <br>
	 *
	 * @param userId 用户id
	 * @return Create author:付湘辉 <br>
	 *         Create on:2008-10-30 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	public String queryUserNameById(Long userId) {
		if (BlankUtil.isBlank(userId)) {
			return null;
		}
		String sqlStr = "SELECT HE.EMPLOYEE_NAME FROM SEC_USER SU JOIN HM_EMPLOYEE HE ON SU.EMPLOYEE_ID = HE.EMPLOYEE_ID WHERE SU.USER_ID = ? LIMIT 1";
		Query query = super.createSQLQuery(sqlStr,userId);
		Object obj = query.uniqueResult();
		return obj == null ? null : (String) obj;
	}
}
